package com.zhudi.develop_demo_as.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zhudi.develop.util.ZhudiDebugLog;
import com.zhudi.develop.util.ZhudiToastSingle;
import com.zhudi.develop_demo_as.BaseActivity;
import com.zhudi.develop_demo_as.MainApplication;
import com.zhudi.develop_demo_as.R;
import com.zhudi.develop_demo_as.adapter.ShowPicContentAdapter;
import com.zhudi.develop_demo_as.ui.activity.pic.PhotoPickerActivity;
import com.zhudi.develop_demo_as.utils.FileUtil;

import org.apache.http.Header;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**显示多图和内容 */
public class ShowPicContentActivity extends BaseActivity{
    private Context context;
    /**选择图片 */
    public final static int REQUEST_CODE = 1;
    private ShowPicContentAdapter photoAdapter;
    private ArrayList<String> selectedPhotos = new ArrayList<>();

    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    @Override
    protected int getLayoutView() {
        return R.layout.activity_show_pic_content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        initView();
    }

    private void initView() {
        tvTitle.setText("编辑");
        photoAdapter = new ShowPicContentAdapter(this, selectedPhotos);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
        recyclerView.setAdapter(photoAdapter);
    }


    @OnClick(R.id.btn)
    public void btn(){
        Intent intentOne = new Intent();
        intentOne.setClass(context,PhotoPickerActivity.class);
        intentOne.putExtra(PhotoPickerActivity.EXTRA_MAX_COUNT, 9);
        startActivityForResult(intentOne, REQUEST_CODE);
    }

    @OnClick(R.id.btnUpload)
    public void upload(){
        if (selectedPhotos.size() == 0){

        } else {
            File[] files = new File[selectedPhotos.size()];
            for (int i = 0;i < selectedPhotos.size();i++){
                File file = new File(selectedPhotos.get(i));
                if (!file.exists()) {
                    ZhudiToastSingle.showToast(MainApplication.getInstance(), getString(R.string.no_img), Toast.LENGTH_SHORT, -1);
                    return;
                }
                // 压缩文件
                file = FileUtil.getUploadFile(file);
                ZhudiDebugLog.d("上传图片大小" + (file.length() / 1024) + "KB");
                files[i] = file;
            }
            RequestParams params = new RequestParams();
            try {
                params.put("file",files);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            new AsyncHttpClient().post(MainApplication.getInstance(), "http://10.21.107.115:8080/javaecshop/data/data!uploadFile", params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    ZhudiDebugLog.d(new String(responseBody));
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    ZhudiDebugLog.d(new String(responseBody));
                }
            });
        }
    }

    public void previewPhoto(Intent intent) {
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<String> photos = null;
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
            }
            selectedPhotos.clear();
            if (photos != null) {
                selectedPhotos.addAll(photos);
            }
            photoAdapter.notifyDataSetChanged();
        }
    }
}
